package com.xoa.controller.workflow;

import com.xoa.model.workflow.FlowTiggerModel;
import com.xoa.service.flowplugin.FlowPluginService;
import com.xoa.service.workflow.flowtigger.FlowTiggerService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * 创建作者:   张勇
 * 创建日期:   2017/6/7 14:35
 * 类介绍  :   触发器
 * 构造参数:
 */
@Controller
@RequestMapping("/tigger")
public class FlowTiggerControlle {

    @Resource
    private FlowTiggerService flowTiggerService;

    @Resource
    private FlowPluginService flowPluginService;



    /**
     * 创建作者:   张勇
     * 创建日期:   2017/6/7 14:45
     * 方法介绍:   新建触发器
     * 参数说明:    触发器实体类
     * @return
     */
    @RequestMapping(value = "saveTigger",produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public @ResponseBody ToJson<FlowTiggerModel> saveTigger(
            FlowTiggerModel flowTiggerModel,
            HttpServletRequest request){
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return flowTiggerService.saveTigger(flowTiggerModel);
    }

    /**
     * 创建作者:   张勇
     * 创建日期:   2017/6/7 14:50
     * 方法介绍:   修改触发器
     * 参数说明:    触发器实体类
     * @return
     */
    @RequestMapping(value = "updateTigger" ,produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    public @ResponseBody ToJson<FlowTiggerModel> updateTigger(FlowTiggerModel flowTiggerModel,
                HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return flowTiggerService.updateTigger(flowTiggerModel);
    }


    /**
     * 创建作者:   张勇
     * 创建日期:   2017/6/7 14:50
     * 方法介绍:   删除触发器
     * 参数说明:    触发器实体类
     * @return
     */
    @RequestMapping(value = "deleteTigger" ,produces = {"application/json;charset=UTF-8"})
    public @ResponseBody ToJson<FlowTiggerModel> deleteTigger(
            @RequestParam(value = "id")Integer id,
            HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return flowTiggerService.deleteTigger(id);
    }

    /**
     * 创建作者:   张勇
     * 创建日期:   2017/7/11 11:41
     * 方法介绍:   所有触发器信息列表展示
     * 参数说明:
     * @return
     */
    @RequestMapping(value = "showPlugin" ,produces = {"application/json;charset=UTF-8"})
    public @ResponseBody ToJson showPlugin(HttpServletRequest request){
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return flowPluginService.showPlugin();
    }


    /**
     * 创建作者:   张勇
     * 创建日期:   2017/7/25
     * 方法介绍:
     * 参数说明:
     * @return
     */
    @RequestMapping(value = "/modifyFieldProperties")
    public @ResponseBody  ToJson<FlowTiggerModel> modifyFieldProperties(String tableName, String id, Integer runId)  {
        return flowTiggerService.modifyFieldProperties(tableName,id,runId);
    }


}
